<?php
namespace Home\Controller;
use Common\Controller\CommonController;

class MenuController extends CommonController {
	function index(){
		$this->display('index');
	}



	//列表页面
	public function menu(){
		$menu=M('menu');
		$list=$menu->where('two_id=0')->order('one_asc desc')->select();//print_r($list);exit;
		foreach ($list as $k=>$v){
			$two_id[] = $v['id'];
		}
		if ($two_id) {
			$w['two_id'] = array('in',$two_id);
			$twomenu = M('menu')->where($w)->order('two_asc desc')->select();//所有二级菜单；二维数组
		}
		foreach ($twomenu as $k=>$v){
			$newcate[$v['two_id']][] = $v;//print_r($v);exit;//所有一级菜单。一维数组
		}
		foreach ($list as $k=>$v){
			$list[$k]['list'] = $newcate[$v['id']];
		}
		//print_r($list);exit;
		$this->assign('list',$list);
		$this->display('index');
	
	}



	//添加显示页面
	public function add_menuindex(){
		$menu=M('menu');
		$list=$menu->where('two_id=0')->select();
		$this->assign('list',$list);
		$this->display('add_menu');
	}



	//添加菜单
	public function add_menu(){//exit('111');
		//接收一级菜单参数
		$type_menu=trim(I('type_menu'));
		$menu=M('menu');
		//print_r($type_menu);
		if ($type_menu=='10086') {// exit('111');
			$one_name=trim(I('name'));
			$one_type=trim(I('type'));
			$one_key=trim(I('key'));
			$one_url=trim(I('url'));
			$one_asc=trim(I('asc'));
			//一级菜单不能超过三个
			$list=$menu->where('two_id=0')->select();
			foreach ($list as $key => $value) {
				$id[]=$value['id'];
			}
			if (count($id)>=3) {
				$this->error('一级菜单不能超过三个');
			}
			$data_one=array(
					'one_name'=>$one_name,
					'one_type'=>$one_type,
					'one_key'=>$one_key,
					'one_url'=>$one_url,
					'one_asc'=>$one_asc,
					'two_id'=>0,
					);
			
			//添加到一级菜单数据库
			$res_add=$menu->data($data_one)->add();
			if (false !== $res_add) {
				 $this->redirect('Menu/menu');
			}else{
				 $this->redirect('Menu/menu');
			}
		}else{//exit('22');
			//接收二级菜单参数			
			$two_name=trim(I('name'));
			$two_type=trim(I('type'));
			$two_key=trim(I('key'));
			$two_url=trim(I('url'));
			$two_asc=trim(I('asc'));

			//判断:如果一级菜单的类型为view；则不能添加二级菜单
			/*$list=$menu->where('two_id=0')->select();
			foreach ($list as $key => $value) {
				$arr[$key]['type']=$value['one_type'];
				$arr[$key]['id']=$value['id'];
				$array=$arr;
			}
			if ($type_menu=$id && $type='view') {
				$this->error('亲，跳转链接的一级菜单不可有二级菜单');
			}
*/
			$data_two=array(
					'two_id'=>$type_menu,
					'two_name'=>$two_name,
					'two_type'=>$two_type,
					'two_key'=>$two_key,
					'two_url'=>$two_url,
					'two_asc'=>$two_asc,
					);
			//添加到2级菜单数据库
			$res_add=$menu->data($data_two)->add();
			if (false !== $res_add) {
				 $this->redirect('Menu/menu');
			}else{
				 $this->redirect('Menu/menu');
			}
		}		
	}



	//修改菜单显示
	public function update_index(){
		$id=I('id');
		$menu=M('menu');
		//$list=$menu->where('two_id=0')->select();
		$list=$menu->where('id='.$id)->find();
		if ($list['two_id']!=0) {
			$arr=$menu->where('id='.$list['two_id'])->find();
			$this->assign('arr',$arr);
		}
		
		$this->assign('data',$list);
		$this->display('update_menu');		
	}




	//修改菜单
	public function update_menu(){
		$id=trim(I('id'));
		$type_menu=trim(I('type_menu'));
		//print_r($type_menu);
		if ($type_menu=='10086') { //exit('111');
			$one_name=trim(I('name'));
			$one_type=trim(I('type'));
			$one_key=trim(I('key'));
			$one_url=trim(I('url'));
			$one_asc=trim(I('asc'));
			$data_one=array(
					'one_name'=>$one_name,
					'one_type'=>$one_type,
					'one_key'=>$one_key,
					'one_url'=>$one_url,
					'one_asc'=>$one_asc,
					'two_id'=>0,
					);
			//实例化菜单
			$menu=M('menu');
			//添加到一级菜单数据库
			$res_save=$menu->where('id='.$id)->data($data_one)->save();
			//echo M()->_sql();exit('@@@');
			if (false !== $res_save) {
			 $this->redirect('Menu/menu');
			}else{
				 $this->redirect('Menu/menu');
			}

		}else{
			//接收二级菜单参数
			//$two_id=I('id');
			$two_name=trim(I('name'));
			$two_type=trim(I('type'));
			$two_key=trim(I('key'));
			$two_url=trim(I('url'));
			$two_asc=trim(I('asc'));
			$data_two=array(
					//'two_id'=>$type_menu,
					'two_name'=>$two_name,
					'two_type'=>$two_type,
					'two_key'=>$two_key,
					'two_url'=>$two_url,
					'two_asc'=>$two_asc,
					);
			//实例化菜单
			$menu=M('menu');
			//添加到2级菜单数据库
			$res_save=$menu->where('id='.$id)->data($data_two)->save();
			//echo M()->_sql();exit('###');
			if (false !== $res_save) {
			 $this->redirect('Menu/menu');
			}else{
				 $this->redirect('Menu/menu');
			}

		}		
		
	}



	//删除菜单
	public function del_menu(){
		$id=I('id');
		$menu=M('menu');
		$list=$menu->where('id='.$id)->find();
		$two_id=$list['two_id'];
		if ($two_id==0) {
			$one_list=$menu->where('two_id='.$id)->select();
			if (!empty($one_list)) {
				$this->error('请先删除二级菜单');
			}
			//exit('22');
			$res_del=$menu->where('id='.$id)->delete();
			if (false !== $res_del) {
			    $this->redirect('Menu/menu');
			}else{
			    $this->redirect('Menu/menu');
			}
		}else{
			$res_del=$menu->where('id='.$id)->delete();
			if (false !== $res_del) {
			    $this->redirect('Menu/menu');
			}else{
			    $this->redirect('Menu/menu');
			}
		}
	}




	//生成菜单
	public function wx_menu(){
		//获取access_token
		//$access_token=access_token();
		//print_r($access_token);exit;
		$menu=M('menu')->where('two_id=0')->order('one_asc desc')->select();//查找出所有一级菜单
		//print_r($menu);exit;
		$array=array();
		foreach ($menu as $key => $value) {
			unset($arr['sub_button']);
			$arr['name'] = $value['one_name'];
			$arr['type'] = $value['one_type'];
			$arr['key'] = $value['one_key'];
			$arr['url'] = $value['one_url'];
			$id=$value['id'];//print_r($id);
			$menu_two=M('menu')->where('two_id='.$id)->order('two_asc desc')->select();

			foreach ($menu_two as $k => $v) {
				if ($v['two_id']=$id) {
					$newtwocate['name']=$v['two_name'];
					$newtwocate['type']=$v['two_type'];
					$newtwocate['key']=$v['two_key'];
					$newtwocate['url']=$v['two_url'];
				
				$arr['sub_button'][]=$newtwocate;

				}
			}	
			$array['button'][]=$arr;
		}
		//使用微信类生成菜单
		$weObj = new \Org\Util\Wechat(C('options'));
		$rs = $weObj->createMenu($array);
		P($rs);
	}





	//获取微信粉丝列表
	public function user_list(){
		$res=user_list();
		if (false !== $res) {
			$this->redirect('Menu/user');
		}else{
			exit('获取列表失败');
		}
	}
	



	//获取微信粉丝
	public function user(){
		$User = M('user'); // 实例化User表
		$keyword=trim(I('keyword'));
	    $where['nickname|openid'] = array('like','%'.$keyword.'%');
		/*$where="(nickname LIKE '%".$keyword."%')";*/
		$count      = $User->where($where)->count();// 查询满足要求的总记录数
		//echo M()->getlastsql();exit('@@@');
		$Page       = new \Think\Page($count,10);// 实例化分页类 传入总记录数和每页显示的记录数(25)
		$show       = $Page->show();// 分页显示输出
		// 进行分页数据查询 注意limit方法的参数要使用Page类的属性
		$list = $User->limit($Page->firstRow.','.$Page->listRows)->where($where)->order('subscribe_time desc')->select();//打印出所有的人
		//echo M()->getlastsql();exit;
		$arr=array('page'=>$show,'list'=>$list,'count'=>$count,'keyw'=>$keyword);
		//print_r($arr);exit;
		$this->assign('list',$arr);
		$this->display('user');
	}	




	//导出csv格式的表格
	public function exportcsv() {
		$keyword=trim(I('keyword'));
		$where['nickname|openid'] = array('like','%'.$keyword.'%');
		$data= M('user')->where($where)->select();
		$str = "序号,姓名,关注,openid,昵称,性别,城市,国家,省份,语言,头像,关注时间,unionid,备注,创建时间,用户组\r\n";
		foreach($data as $key=>$value){
			$id = $value['id'];
			$name = $value['name'];
			$subscribe = $value['subscribe'];
			$openid = $value['openid'];
			$nickname = $value['nickname'];
			$sex = $value['sex'];
			$city = $value['city'];
			$country = $value['country'];
			$province = $value['province'];
			$language = $value['language'];
			$headimgurl = $value['headimgurl'];
			$subscribe_time = $value['subscribe_time'];
			$unionid = $value['unionid'];
			$remark = $value['remark'];
			$ctime = date('Y-m-d H:i:s',$value['ctime']);
			$groupid = $value['groupid'];
			$str .= "".$id.",".$name.",".$subscribe.",".$openid.",".$nickname.",".$sex.",".$city.",".$country.",".$province.",".$language.",".$headimgurl.",".$subscribe_time.",".$unionid.",".$remark.",".$ctime.",".$groupid."\r\n";
			//$str .="".$name.",".$section1.",".$phone.",".$section_name.",".$ordertime.",".$content.",".$addtime.",".$idcard."\r\n";
		}
		$filename = date('Ymd').'.csv'; //设置文件名
		if(filesize($file)>1024){
			$file = getcwd().'/aaa.csv';
			//dump($file);exit;
			file_put_contents($file,$str);
			//dump($str);exit;
			if (file_exists($file)) {
					$filesize=filesize($file)+1000;
					
					header('Content-Description:File Transfer');
					header('Content-type:'.csv);
					header('Content-Transfer-Encoding:binary');
					header("Accept-Ranges: bytes");
					header('Expires:0');
					header('Cache-Control:must-revalidate');
					header('Pragma:public');
					header("Content-Length:".$filesize);
					header('Content-Disposition: attachment; filename='.$filename);
						//打开文件
						$fp = fopen($file, "rb");
						//dump($fp);exit;
						//设置指针位置
						fseek($fp,0);
						while (!feof($fp)) {
							//虚幻输出
							//设置文件最长执行时间
							set_time_limit(0);
							print (fread($fp, 1024 * 150)); //输出文件
							flush(); //输出缓冲
							ob_flush();
						}
						fclose($fp);
					
					unlink($file);
					exit ();
				}else{
					echo "err";die();
				}
			}else{
				header("Content-type:text/csv");
				header("Content-Disposition:attachment;filename=".$filename);
				header('Cache-Control:must-revalidate,post-check=0,pre-check=0');
				header('Expires:0');
				header('Pragma:public');
				$str = iconv('utf-8', 'gb2312', $str);
				echo $str;
			}
		} 	


}